﻿using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.Domain
{
    public static class ExcelCustomCellFactory
    {
        public static Cell TextCell(string header, string text, int index)
        {
            Cell cell = new Cell();
            cell.DataType = CellValues.InlineString;
            cell.CellReference = header + index;
            cell.InlineString = new InlineString { Text = new Text { Text = text } };
            return cell;
        }

        public static Cell NumberCell(string header, string text, int index)
        {
            Cell cell = new Cell();
            cell.DataType = CellValues.Number;
            cell.CellReference = header + index;
            cell.CellValue = new CellValue(text);
            return cell;
        }

        public static Cell FormatedNumberCell(string header, string text, int index)
        {
            Cell cell = new Cell();
            cell.DataType = CellValues.Number;
            cell.CellReference = header + index;
            cell.CellValue = new CellValue(text);
            cell.StyleIndex = 2;
            return cell;
        }

        public static Cell DateCell(string header, DateTime dateTime, int index)
        {
            Cell cell = new Cell();
            cell.DataType = CellValues.Date;
            cell.CellReference = header + index;
            cell.StyleIndex = 1;
            cell.CellValue = new CellValue { Text = dateTime.ToOADate().ToString() }; ;
            return cell;
        }
        public static Cell FomulaCell(string header, string text, int index)
        {
            Cell cell = new Cell();
            cell.CellFormula = new CellFormula { CalculateCell = true, Text = text };
            cell.DataType = CellValues.Number;
            cell.CellReference = header + index;
            cell.StyleIndex = 2;
            return cell;
        }

        public static Cell HeaderCell(string header, string text, int index)
        {
            Cell cell = ExcelCustomCellFactory.TextCell(header, text, index);
            cell.StyleIndex = 11;
            return cell;
        }

        public static Cell AutoCreateCell(object fieldValue, string header, int index)
        {
            Type fieldType = fieldValue.GetType();
            if (fieldType == typeof(string))
            {
                return TextCell(header, fieldValue.ToString(), index);
            }
            else
                if (fieldType == typeof(DateTime))
                {
                    return DateCell(header, Convert.ToDateTime(fieldValue), index);
                }
                else
                    if (fieldType == typeof(decimal) || fieldType == typeof(double))
                    {
                        return FormatedNumberCell(header, fieldValue.ToString(), index);
                    }
                    else
                    {
                        long value;
                        if (long.TryParse(fieldValue.ToString(), out value))
                        {
                            return NumberCell(header, fieldValue.ToString(), index);
                        }
                        else
                        {
                            return TextCell(header, fieldValue.ToString(), index);
                        }
                    }
        }
    }
}
